package com.yb.bussiness.shoper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.goods.TgFavorable;
import com.yb.domain.goods.TpBuyRecord;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.framework.PageUtil;

public class TgFavorableServiceImpl implements TgFavorableService {

	private IBaseDao<TgFavorable, Long> dao;

	private String FIND_SQL = "SELECT * FROM t_g_favorable where 1=1 ";

	public TgFavorableServiceImpl() {
		dao = new BaseDao<TgFavorable, Long>(TgFavorable.class);
	}

	public void delTgStore(Long id) {
		dao.deleteById(id);
	}

	public PageUtil findList(Map<String, String> map, int page, int row) {
		return null;
	}

	public TgFavorable read(Long id) {
		return dao.read(id);
	}

	public void saveOrUpdate(TgFavorable obj) {
		if (0 == obj.getId()){
		    obj.setCreateTime(new Date());
			dao.saveEntity(obj);
	    }else{
			dao.update(obj);
	    }
	}

	public TgFavorable findStoreBybid(Long lid) {
		String sql = FIND_SQL + " and brandId=?";
		List<TgFavorable> list = dao.findList(sql, new Object[] { lid });
		if(null!=list && list.size()>0)
		return list.get(0);
		return null;
	}

	public int count(Map<String, String> parameMap) {
		int count = 0;
		try {
			String str = "  SELECT count(*) FROM t_g_favorable b WHERE b.brandId=?";
			if (parameMap.get("name") != null
					&& !"".equals(parameMap.get("name"))) {
				str += "  and b.title LIKE   '%" + parameMap.get("name") + "%'";
			}
			if (parameMap.get("auding") != null
					&& !"".equals(parameMap.get("auding"))) {
				if ("0".equals(parameMap.get("auding")))
					str += "  and (b.auding = " + parameMap.get("auding")
							+ " or b.auding is null) ";
				else
					str += "  and b.auding = " + parameMap.get("auding");
			}
			List<Object[]> objAryList = dao.findObjectArryList(str,new Object[]{parameMap.get("brandId")});
			if (null != objAryList && objAryList.size() > 0) {
				Object[] obj = objAryList.get(0);
				count = new Integer(obj[0].toString());
			}
		} catch (Exception e) {
			return 0;
		}
		return count;
	}

	public List<TgFavorable> findFavorable(Map<String, String> parameMap) {
		try {
			String str = " SELECT * FROM t_g_favorable b WHERE b.brandId=?";
			if (parameMap.get("name") != null
					&& !"".equals(parameMap.get("name"))) {
				str += "  and b.title LIKE   '%" + parameMap.get("name") + "%'";
			}
			if (parameMap.get("auding") != null
					&& !"".equals(parameMap.get("auding"))) {
				if ("0".equals(parameMap.get("auding")))
					str += "  and (b.autding = " + parameMap.get("auding")
							+ " or b.autding is null) ";
				else
					str += "  and b.autding = " + parameMap.get("auding");
			}
			str += " order by createTime desc ";
			if (parameMap.get("page") != null && parameMap.get("row") != null) {
				int start = Integer.parseInt(parameMap.get("page")) - 1;
				int limit = Integer.parseInt(parameMap.get("row"));
				str += " limit " + start * limit + "," + limit;
			}
			List<TgFavorable> list = dao.findList(str,new Object[]{parameMap.get("brandId")});
			if (list != null && !list.isEmpty()) {
				List<TgFavorable> newlist = new ArrayList<TgFavorable>();
				for (TgFavorable tpRecord : list) {
					 tpRecord.setStartTimeString(DateTimeUtil.switchDateToString(tpRecord.getStartTime(),"yyyy-MM-dd HH:mm:ss"));
					tpRecord.setEndTimeString(DateTimeUtil.switchDateToString(tpRecord.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
					newlist.add(tpRecord);
				}
				return newlist;
			}
			return null;
		} catch (Exception e) {
			return null;
		}
	}

}
